#include "precompiled.h"
#include <ClanLib/gl.h>
#include <ClanLib/sound.h>
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/gui.h>
#include "MainWindow.h"

#include <iostream>
using namespace std;

//////////////////////////////
template <typename Type>
class SharedPtr : public CL_SharedPtr_Link
{
public:
	SharedPtr()
	: ptr(0)
	{
	}

	SharedPtr(const SharedPtr<Type> &copy)
	: ptr(0)
	{
		connect(copy);
		ptr = copy.ptr;
	}

	explicit SharedPtr(const CL_UnknownSharedPtr &copy)
	: ptr(0)
	{
		connect(copy);


		ptr = (Type *) copy.ptr;
	}

	template <typename InitType>
	explicit SharedPtr(InitType *ptr)
	: ptr(ptr)
	{
		mutex = CL_System::get_sharedptr_mutex();
		if (ptr)
			create_deleter(ptr);
	}

	template <typename InitType> //my
	SharedPtr(const SharedPtr<InitType>& copy)
	{
		connect(copy);
		ptr = copy.ptr;
	}

	SharedPtr &operator =(const SharedPtr &copy)
	{
		if (this == &copy)
			return *this;
		disconnect();
		connect(copy);
		ptr = copy.ptr;
		return *this;
	}

	operator Type *() { return ptr; }

	operator const Type *() const { return ptr; }

public:
	Type *ptr;

};
//////////////////////////////

static int clanlib_main(const std::vector<CL_String>& args)
{
	try
	{
		CL_SetupCore setup_core;
		CL_SetupDisplay setup_display;
		CL_SetupGL setup_gl;
		CL_SetupSound setup_sound;
		CL_SetupGUI setup_gui;

		CL_ConsoleWindow console_window("error console");

		CL_DisplayWindowDescription desc;
		desc.set_size(CL_Size(640, 480), true /* exclude window frame */);
		desc.set_title("MusicLife");
		desc.set_allow_resize(true);
		CL_GUIManager gui_manager;

		//CL_Font_System::register_font("Resources/visitor1.ttf", "visitor");

		CL_GUIWindowManagerSystem wm;
		gui_manager.set_window_manager(wm);

		CL_GUIThemeDefault theme;
		CL_ResourceManager resources("Resources/GUIThemeAero/resources.xml");
		theme.set_resources(resources);
		gui_manager.set_theme(theme); 
		//gui_manager.set_css_document("Resources/GUIThemeAero/theme.css");

		MainWindow main_window(&gui_manager, desc);
		gui_manager.exec();
	}
	catch(CL_Exception & exception)
	{
		//CL_Console::write_line("Exception caught: " + exception.get_message_and_stack_trace());
		string s = "Exception caught: " + exception.get_message_and_stack_trace();
		MessageBox(0, s.c_str(), 0, MB_OK);
	}
	return 0;
	int i;
/////////////
	CL_SharedPtr<int> p(&i);
//	CL_SharedPtr<const int> c = p;
	//SharedPtr<const int> c(p);
}

CL_ClanApplication app(clanlib_main);
